package com.fishingwithme.android.ui.screens.map

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.Log
import androidx.core.graphics.createBitmap
import androidx.core.graphics.withTranslation
import com.fishingwithme.android.R
import com.tencent.tencentmap.mapsdk.maps.model.BitmapDescriptor
import com.tencent.tencentmap.mapsdk.maps.model.BitmapDescriptorFactory

/**
 * Custom marker drawer that uses Canvas to create markers with spot names and fishing icon
 */
class CustomMarkerDrawer {

    /**
     * Create a custom marker bitmap with the spot name and fishing icon
     */
    fun createMarkerBitmap(spotName: String, context: android.content.Context): BitmapDescriptor? {
        return try {
            // Define dimensions for the marker
            val textPaint = Paint().apply {
                color = Color.BLACK
                textSize = 36f // Adjust text size as needed
                isAntiAlias = true
                textAlign = Paint.Align.CENTER
            }

            // Measure text dimensions
            val textBounds = Rect()
            textPaint.getTextBounds(spotName, 0, spotName.length, textBounds)

            // Load the fishing icon
            val iconDrawable = context.resources.getDrawable(R.mipmap.go_fishing, null)

            // Set icon dimensions to match the default marker size (typically 48x48 dp)
            val iconWidth = 80
            val iconHeight = 80

            // Scale the drawable to the desired size
            iconDrawable.setBounds(0, 0, iconWidth, iconHeight)

            // Add padding around the text and icon
            val padding = 20
            val textWidth = textBounds.width()
            val textHeight = textBounds.height()

            // Calculate total width and height
            val totalWidth = kotlin.math.max(textWidth, iconWidth) + padding * 2
            val totalHeight =
                iconHeight + textHeight + padding * 3 // Extra padding between icon and text

            // Ensure minimum dimensions
            val minWidth = 120
            val minHeight = 100
            val finalWidth = kotlin.math.max(totalWidth, minWidth)
            val finalHeight = kotlin.math.max(totalHeight, minHeight)

            // Create bitmap with transparent background
            val bitmap = createBitmap(finalWidth, finalHeight)
            val canvas = Canvas(bitmap)

            // Skip drawing background and border for completely transparent background

            // Draw icon centered at the top
            val iconLeft = (finalWidth - iconWidth) / 2
            val iconTop = padding
            canvas.withTranslation(iconLeft.toFloat(), iconTop.toFloat()) {
                iconDrawable.draw(this)
            }

            // Draw text centered below the icon
            val x = finalWidth / 2f
            val y = iconTop + iconHeight + padding + textHeight / 2f
            canvas.drawText(spotName, x, y, textPaint)

            // Convert to BitmapDescriptor
            BitmapDescriptorFactory.fromBitmap(bitmap)
        } catch (e: Exception) {
            Log.e("CustomMarkerDrawer", "Error creating custom marker bitmap", e)
            null
        }
    }
}